package com.ruoyi.yuan.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.yuan.domain.City;
import com.ruoyi.yuan.mapper.ProvinceMapper;
import com.ruoyi.yuan.domain.Province;
import com.ruoyi.yuan.service.IProvinceService;

/**
 * 省信息Service业务层处理
 * 
 * @author guifeng
 * @date 2022-08-25
 */
@Service
public class ProvinceServiceImpl implements IProvinceService 
{
    @Autowired
    private ProvinceMapper provinceMapper;

    /**
     * 查询省信息
     * 
     * @param provinceId 省信息主键
     * @return 省信息
     */
    @Override
    public Province selectProvinceByProvinceId(Long provinceId)
    {
        return provinceMapper.selectProvinceByProvinceId(provinceId);
    }

    /**
     * 查询省信息列表
     * 
     * @param province 省信息
     * @return 省信息
     */
    @Override
    public List<Province> selectProvinceList(Province province)
    {
        return provinceMapper.selectProvinceList(province);
    }

    /**
     * 新增省信息
     * 
     * @param province 省信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProvince(Province province)
    {
        int rows = provinceMapper.insertProvince(province);
        insertCity(province);
        return rows;
    }

    /**
     * 修改省信息
     * 
     * @param province 省信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProvince(Province province)
    {
        provinceMapper.deleteCityByProvinceId(province.getProvinceId());
        insertCity(province);
        return provinceMapper.updateProvince(province);
    }

    /**
     * 批量删除省信息
     * 
     * @param provinceIds 需要删除的省信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProvinceByProvinceIds(Long[] provinceIds)
    {
        provinceMapper.deleteCityByProvinceIds(provinceIds);
        return provinceMapper.deleteProvinceByProvinceIds(provinceIds);
    }

    /**
     * 删除省信息信息
     * 
     * @param provinceId 省信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProvinceByProvinceId(Long provinceId)
    {
        provinceMapper.deleteCityByProvinceId(provinceId);
        return provinceMapper.deleteProvinceByProvinceId(provinceId);
    }

    /**
     * 新增市信息表信息
     * 
     * @param province 省信息对象
     */
    public void insertCity(Province province)
    {
        List<City> cityList = province.getCityList();
        Long provinceId = province.getProvinceId();
        if (StringUtils.isNotNull(cityList))
        {
            List<City> list = new ArrayList<City>();
            for (City city : cityList)
            {
                city.setProvinceId(provinceId);
                list.add(city);
            }
            if (list.size() > 0)
            {
                provinceMapper.batchCity(list);
            }
        }
    }
}
